package request2_p2p;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * @ClassName OrderSubscriber
 * @Description TODO
 * @Author MaQingpin
 * @Date 2020/11/5 10:03
 * @Version 1.0
 */
public class MOrderSubscriber implements Observer {
    private List<Rider> subscribers=new ArrayList<>();
    public MOrderSubscriber(List<Rider> subscribers){
        this.subscribers=subscribers;
    }

    public List<Rider> getSubscribers() {
        return subscribers;
    }

    public void setSubscribers(List<Rider> subscribers) {
        this.subscribers = subscribers;
    }

    @Override
    public void update(Observable o, Object arg) {

            MOrderPublisher dispatch=(MOrderPublisher) o;
            COrderEvent orderEvent=(COrderEvent)arg;
            Merchant merchant=dispatch.getMerchant();
            Rider final_rider=null;
            double min=9999;
            for(Rider rider:subscribers){
                Double distance=Math.sqrt(Math.pow((rider.getLocation()[0]-merchant.getLocation()[0]),2)+Math.pow((rider.getLocation()[1]-merchant.getLocation()[1]),2));
                System.out.println("骑手"+rider.getName()+"您好！");
                StringBuffer s=new StringBuffer();
                for(Product productList :orderEvent.getProductList()){
                    s.append(productList.getName()+" ");
                }
                String s1=s.toString();
                System.out.println("接收到来自"+dispatch.getMerchant().getName()+"的订单配送任务："+s);
                System.out.println("商家距您："+distance+"公里");
                System.out.println("-----------------------------");
                if(distance<min){
                    min=distance;
                    final_rider=rider;
                }
            }
            System.out.println("事件分发完毕，最终分配的骑手为："+final_rider.getName()+" id为"+final_rider.getId());


    }
}
